package com.zhilianbao.leyaogo.ui.adapter.shoppingcart;

import android.content.Context;

import com.bql.animationcheckbox.AnimationCheckBox;
import com.bql.baseadapter.recycleView.QuickRcvAdapter;
import com.bql.baseadapter.recycleView.QuickRcvHolder;
import com.bql.utils.DateUtils;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.model.response.Coupon.CanUseCard;
import com.zhilianbao.leyaogo.view.widgets.MoneyTextView;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * Author: hcy
 * Description:可用优惠券
 * Created: 2016/11/30 15:41
 */
public class CardCanUseAdapter extends QuickRcvAdapter<CanUseCard> {

    //    private Set<Integer> cardIds = new HashSet<>();
    private List<CanUseCard> mListData;

    public CardCanUseAdapter(Context context, List<CanUseCard> data) {
        super(context, data, R.layout.item_card_can_use);
        mListData = data;
    }

    @Override
    protected void bindDataHelper(QuickRcvHolder viewHolder, int position, CanUseCard item) {
        double cardPrice = item.getCardPrice();
        double balancePrice = item.getCardBalance();
        if (cardPrice >= 10000) {
            BigDecimal bdBalance = new BigDecimal(cardPrice);
            BigDecimal bigDecimalBalance = bdBalance.setScale(2, RoundingMode.HALF_UP);
            BigDecimal bdPrice = new BigDecimal(balancePrice);
            BigDecimal bigDecimalPrice = bdPrice.setScale(2, RoundingMode.HALF_UP);
            cardPrice = bigDecimalPrice.doubleValue();
            balancePrice = bigDecimalBalance.doubleValue();
        }
        String cardNo = item.getCardNo();
        String startDay = DateUtils.format(Long.valueOf(item.getBeginDay()), "yyyy.MM.dd");
        String endDay = DateUtils.format(Long.valueOf(item.getEndDay()), "yyyy.MM.dd");
        viewHolder.setText(R.id.card_max_num, mContext.getString(R.string.amount) + cardPrice + mContext.getString(R.string.money));
        viewHolder.setText(R.id.card_use_day, mContext.getString(R.string.use_day) + startDay + mContext.getString(R.string.sub) + endDay);
        MoneyTextView mtv = viewHolder.getView(R.id.mtv);
        mtv.setAmount(String.valueOf(balancePrice));
        //实物卡
        if (item.getCardItemType() == 0) {
            viewHolder.setVisible(R.id.tv_card_num, true);
            viewHolder.setVisible(R.id.ll_recharge_sale_card, false);
            viewHolder.setText(R.id.tv_card_num, mContext.getString(R.string.card_mun) + cardNo);
        }
        //网销卡
        else {
            viewHolder.setVisible(R.id.tv_card_num, false);
            viewHolder.setVisible(R.id.ll_recharge_sale_card, true);
            viewHolder.setText(R.id.tv_card_des, item.getCardPwd());
            viewHolder.setText(R.id.tv_card_percent, mContext.getString(R.string.deduction_percent, item.getOrderPayLimit() + ""));
        }
        AnimationCheckBox checkBox = viewHolder.getView(R.id.checkbox);
        checkBox.setChecked(item.isCheck(), false, false);

        checkBox.setTag(position);
        checkBox.setOnCheckedChangeListener(new AnimationCheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(AnimationCheckBox checkBox, boolean isChecked) {
                int pos = (int) checkBox.getTag();
                mListData.get(pos).setCheck(isChecked);
            }
        });
    }

}
